package com.easybenefit.UUService.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;

import com.easybeanfit.UUService.commutill.UUDialogUtill;
import com.easybeanfit.UUService.commutill.UUJsonUtil;
import com.easybeanfit.UUService.commutill.UUNetUtill;
import com.easybeanfit.UUService.commutill.UUSharedUtil;
import com.easybeanfit.UUService.commutill.UUToastUtil;
import com.easybeanfit.UUService.config.UUConfig;
import com.easybeanfit.UUService.config.UUDict;
import com.easybenefit.UUService.BaseActivity;
import com.easybenefit.UUService.R;
import com.easybenefit.UUService.adapter.CouponsListAdapter;
import com.easybenefit.UUService.model.CouponlistBean;
import com.easybenefit.UUService.model.HttpCouponBean;
import com.easybenefit.UUService.model.UserInfo;
import com.easybenefit.UUService.myinterface.IActivityInterface;
import com.easybenefit.UUService.xlist.XListView;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.List;

/**
 * 派发优惠券列表
 */
public class CouponsDistributedListActivity extends BaseActivity implements IActivityInterface, View.OnClickListener {
    private TextView tv_title;//标题
    private XListView xListView;
    private TextView btn_add;
    private CouponsListAdapter couponsListAdapter;
    private Button btn_back;
    private List<CouponlistBean> list;
    private int page = 1;
    private int rows = 10;
    private Context context;
    private ProgressDialog progressDialog;
    /**
     * 监听list事件
     */
    private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            CouponlistBean bean=list.get(position-1);
            Intent intent=new Intent(CouponsDistributedListActivity.this,CouponsDetailActivity.class);
            intent.putExtra("type",true);
            intent.putExtra("url",bean.getUrl());
            intent.putExtra("umeng_title",bean.getSharename());
            intent.putExtra("umeng_img",bean.getShareimg());
            intent.putExtra("umeng_url",bean.getShareurl());
            intent.putExtra("coupon_id",bean.getCoupon_id());
            startActivity(intent);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_distributed_coupons_list);
        initView();
        initData();
        initListener();
    }

    @Override
    public void initData() {
        context = this;
        list = new ArrayList<CouponlistBean>();
        couponsListAdapter = new CouponsListAdapter(list, context,1);
        tv_title.setText("派发优惠券");

        xListView.setAdapter(couponsListAdapter);
        getCouponListData();
    }

    @Override
    public void initView() {
        xListView = (XListView) findViewById(R.id.xlist_coupons);
        tv_title = (TextView) findViewById(R.id.top_title);
        btn_back = (Button) findViewById(R.id.btn_back);
        btn_add = (TextView) findViewById(R.id.btn_add);

        btn_add.setVisibility(View.GONE);
        xListView.setPullLoadEnable(false);
        xListView.setPullRefreshEnable(true);

    }

    /**
     * 加载结束需要执行的方法
     */
    public void onload() {
        xListView.stopRefresh();
        xListView.stopLoadMore();
        xListView.setRefreshTime();
    }

    @Override
    public void initListener() {
        btn_back.setOnClickListener(this);
        xListView.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {
                page = 1;
                list.clear();
                getCouponListData();
                onload();
            }

            @Override
            public void onLoadMore() {
                page++;

                onload();
            }
        });
        xListView.setOnItemClickListener(onItemClickListener);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_back:
                finish();
                break;

        }
    }

    /**
     * 获取可以派发的优惠券数据
     */
    public void getCouponListData(){
        if (!UUNetUtill.isConnected(CouponsDistributedListActivity.this)) {
            UUToastUtil.showToast(CouponsDistributedListActivity.this, UUDict.NOT_INTERNET);
        } else {
            String url = UUConfig.SERVER_BASE_URL + "merdistrlist";
            RequestParams params = new RequestParams();
            UserInfo userInfo = (UserInfo) UUSharedUtil.getObject(context, UUConfig.UU_USERINFO_NAME, UserInfo.class);
            params.put("mer_id", userInfo.getMer_id());
            AsyncHttpClient client = new AsyncHttpClient();
            client.post(url, params, new AsyncHttpResponseHandler() {
                @Override
                public void onStart() {
                    progressDialog = UUDialogUtill.getLoadingBar(CouponsDistributedListActivity.this, "加载中....");
                    progressDialog.show();
                }

                @Override
                public void onFinish() {
                    progressDialog.dismiss();
                }

                @Override
                public void onSuccess(int i, Header[] headers, byte[] bytes) {
                    String content = new String(bytes);
                    HttpCouponBean responseBean = (HttpCouponBean) UUJsonUtil.fromJson(content, HttpCouponBean.class);
                    list.addAll(responseBean.getData().getCouponlist());
                    couponsListAdapter.notifyDataSetChanged();
                }

                @Override
                public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {

                }
            });
        }
    }
}
